package com.BlockingQueue;


import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

/**
 * @author Zhang Guangyong
 * @desacription
 * @date 2019年08月20日 9:40
 */
class Producer extends Thread {

    private BlockingQueue<String> bq;

    public Producer(BlockingQueue<String> bq) {

        this.bq = bq;
    }

    public void run() {
        String[] strings = new String[]{"dsfs", "afd", "fdasf"};

        for (int i = 0; i < 9999999; i++) {
            System.out.println(getName() + "生产者准备生产集合元素！");

            try {
                Thread.sleep(200);
                bq.put(strings[1 % 3]);
            } catch (Exception e) {
                e.getMessage();
            }
        }
    }
}

class Consumer extends Thread {

    private BlockingQueue<String> bq;

    public Consumer(BlockingQueue<String> bq) {
        this.bq = bq;
    }

    public void run() {
        while (true) {
            System.out.println(getName() + "消费者消费集合元素");
            try {
                Thread.sleep(200);
                bq.take();
            } catch (Exception e) {
                e.getMessage();
            }

            System.out.println(getName() + "消费完成" + bq);
        }


    }

}


public class BlockingQueueTest2 {

    public static void main(String[] args) {

        BlockingQueue<String> bq = new ArrayBlockingQueue<>(2);

        new Producer(bq).start();
        new Producer(bq).start();
        new Producer(bq).start();

        new Consumer(bq).start();

    }


}
